package com.sky.utils;


public class GeoUtil {

    /**
     * 计算两个地理位置之间的距离（单位：米）
     *
     * @param lat1 第一个地点的纬度
     * @param lon1 第一个地点的经度
     * @param lat2 第二个地点的纬度
     * @param lon2 第二个地点的经度
     * @return 两点之间的距离（米）
     */
    public static double distance(double lat1, double lon1, double lat2, double lon2) {
        int R = 6371; // 地球半径，单位：千米
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
                Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
                        Math.sin(dLon / 2) * Math.sin(dLon / 2);
        double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
        double distance = R * c * 1000; // 转换为米
        return distance;
    }

    /**
     * 查找最近的校园
     *
     * @param lat 用户的纬度
     * @param lon 用户的经度
     * @param SchoolInfoes 所有校园的列表
     * @return 最近的校园信息
     */
   /* public static SchoolInfo findNearestSchoolInfo(double lat, double lon, List<SchoolInfo> SchoolInfoes) {
        SchoolInfo nearestSchoolInfo = null;
        double minDistance = Double.MAX_VALUE;

        for (SchoolInfo SchoolInfo : SchoolInfoes) {
            double distance = distance(lat, lon, SchoolInfo.getLat(), SchoolInfo.getLng());
            if (distance < minDistance) {
                minDistance = distance;
                nearestSchoolInfo = SchoolInfo;
            }
        }

        if (nearestSchoolInfo == null) {
            throw new CampusNotFoundException(MessageConstant.CAMPUS_NOT_FOUND_NEARBY);
        }

        return nearestSchoolInfo;
    }*/
}
